package com.erhirent.view;

import java.util.List;
import java.util.Scanner;

import com.erhairent.tools.InputTools;
import com.erhirent.bean.Car;

public class RentView extends View {

	Scanner mScanner = new Scanner(System.in);

	int userID;

	@Override
	public View showView() {
		// 租车 改状态
		// 所有可上架车辆
		List<Car> list1 = mcar.userquery();
		if (list1.size() != 0) {
			System.out.println("----------------------------");
			System.out.println("汽车编号\t" + "汽车名称\t" + "备注\t" + "品牌\t" + "类型\t" + "单价\t" + "是否可租");
			for (Car car : list1) {
				System.out.print(car.getcarId() + "\t");
				System.out.print(car.getcarName() + "\t");
				System.out.print(car.getcarRemark() + "\t");
				System.out.print(car.getbrandName() + "\t");
				System.out.print(car.gettypeName() + "\t");
				System.out.print(car.getcarPrice() + "/天\t");
				System.out.println(car.getcarIsrent() + "\t");
			}
			System.out.println("----------------------------");
		} else {
			System.out.println("获取车辆信息失败");
		}

		System.out.println("输入您需要租车的车辆编号:");
		int carID =0;
		boolean isrent = true;
		for (; isrent;) {
			// 所有可借车辆
			carID = InputTools.nextInt();
			if (mcar.userqueryRent(carID)) {
				isrent = false;
			} else {
				System.out.println("该车不可借,请重新选择:");
				
			}
		}
		String name = null;
		boolean isContinue = true;
		for (; isContinue;) {
			System.out.println("输入您的账号:");
			name = mScanner.next();
			System.out.println("请输入密码 :");
			String Pwd = mScanner.next();
			// 判断用户是否存在
			if (muser.isSame(name, Pwd)) {

				System.out.println("这是你的账户名?1.是  2.否");
				System.out.println(name);
				int c = InputTools.nextInt();;
				if (c == 1) {
					isContinue = false;
				} else {
					System.out.println("重新输入:");
				}
			} else {
				System.out.println("密码错误");
			}
		}

		// 添加还车时间
		// 将车辆编号为a的汽车状态改成 不可借 还车时改回来
		// 显示租车的车辆信息 向租车记录中加一条信息
		// 得到用户名

		userID = muser.GetUserId(name);
		// 得到单价
		double price = mcar.getCarPrice(carID);
		boolean is =  true;
		int day = 0;
		System.out.println("您需要租的天数:");
		for(;is;) {
					
			day = InputTools.nextInt();

			if(day>0) {
				is= false;
				
			}
			System.out.println("输入正确天数:");
		}
		
		boolean istrue = true;

		for (; istrue;) {

			// 总价
			double pri = price * day;
			// 记录表放数据
			if (mcar.userRecord(carID, userID, pri, day)) {
				System.out.println("租车成功!");
				// 跳出租车记录界面一条记录
				List<Car> list2 = mcar.userqueryoneRecord(userID);
				if (list2.size() != 0) {
					System.out.println("------------------------------------");
					System.out
							.println("汽车编号\t" + "汽车名称\t" + "总价\t" + "备注\t" + "品牌\t" + "类型\t" + "租车时间\t\t\t\t" + "还车时间");
					for (Car car : list2) {
						System.out.print(car.getcarId() + "\t");
						System.out.print(car.getcarName() + "\t");
						System.out.print(car.getrentTotalPrice() + "\t");
						System.out.print(car.getcarRemark() + "\t");
						System.out.print(car.getbrandName() + "\t");
						System.out.print(car.gettypeName() + "\t");
						System.out.print(car.getrentTime() + "\t");
						System.out.println(car.getreturnTime() + "\t");
					}
					System.out.println("------------------------------------");
				} else {
					System.out.println("获取记录失败");
				}

				istrue = false;
			} else {
				System.out.println("租车失败");
				System.out.println("1.继续    2.退出");
				int choose = InputTools.nextInt();
				if (choose == 2) {
					return mView;
				}
			}
		}
		return mView;
	}

}
